<?php

class MenuItemsController extends AppController {

    var $name = 'MenuItems';
    var $components = array('WritingPad');
    var $helpers = array('Flash');

    function index() {
        $this->MenuItem->recursive = 0;
        $this->set('menuItems', $this->paginate());
    }

    /**
     * This function generates a unique order token.
     * Every token is prepended with "ali_". It is not necessary.
     * @return <type> unique order token
     */
    function generateUniqueToken() {
        return uniqid("ali_");
    }

    /**
     * This function will return unique order token
     *
     * This number will be used to identify temporary orders in the database.
     *
     * @return <type>
     */
    function getUniqueOrderToken() {
        if ($this->Session->check('OrderToken')) {
            $uniqueOrderToken = $this->Session->read('OrderToken');
        } else {
            $uniqueOrderToken = $this->generateUniqueToken();
        }
        return $uniqueOrderToken;
    }

    function showMenu() {
        $this->MenuItem->recursive = 0;
        $this->set('menuItems', $this->paginate());

        //get a unique order token
        $orderToken = $this->getUniqueOrderToken();
        $this->Session->write('OrderToken', $orderToken);
        $this->set('uniqueOrderToken', $orderToken);

        //$output = $this->WritingPad->readPad($uniqueOrderToken);//$output . " - [" . $uniqueOrderToken . "]";
        $output = $this->WritingPad->getHumanReadableOrder($orderToken);
        $this->set('output', $output);
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid menu item', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('menuItem', $this->MenuItem->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->MenuItem->create();
            if ($this->MenuItem->save($this->data)) {
                $this->Session->setFlash(__('The menu item has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The menu item could not be saved. Please, try again.', true));
            }
        }
        $menuItemGroups = $this->MenuItem->MenuItemGroup->find('list');
        $this->set(compact('menuItemGroups'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid menu item', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->MenuItem->save($this->data)) {
                $this->Session->setFlash(__('The menu item has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The menu item could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->MenuItem->read(null, $id);
        }
        $menuItemGroups = $this->MenuItem->MenuItemGroup->find('list');
        $this->set(compact('menuItemGroups'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for menu item', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->MenuItem->delete($id)) {
            $this->Session->setFlash(__('Menu item deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Menu item was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function getNameOfMenuItem($menu_item_id) {
        $retValue = $this->MenuItem->findById($menu_item_id, array(
                    'recursive' => 0
                ));
//            debug($retValue);
        return $retValue;
    }

}

?>